import { Component, Input, OnInit } from '@angular/core';
import {SvjackService} from '@src/app/service/svjack.service'
import {  ButtonConfig } from "@src/app/model/jack.model";

@Component({
  selector: 'jack-button',
  templateUrl: './button.component.html',
  styleUrls: ['./button.component.css']
})



export class ButtonComponent implements OnInit {

  @Input() conf:ButtonConfig;

  //界面的config
  ui:any= {
    loading:'loaded',
    show:true,
    loadingText:'',
    context:{
      $implict:'world',
      locakSk:'svet'
    },
    disabled: false,
  }
  constructor(
    private svjack: SvjackService
  ) { }

  ngOnInit(): void {
    Object.assign(this.ui, this.conf)
  }

  ngAfterViewInit() {
  }


  getConf() {
    // 获取配置

  }

  onclick () {
    if(!this.conf) {
      return;
    }
    if(this.conf.requestName) {
      this.ui.loading = 'loading'
      this.ui.disabled = true;
      this.svjack.apiRequest(this.conf.requestName).then(res => {
        this.conf.onsuccess && this.conf.onsuccess(res)
        this.ui.loading = 'loaded'
        this.ui.disabled = false;
      }).catch(e => {
        this.conf.onfail && this.conf.onfail(e)
        this.ui.loading = 'loaded'
        this.ui.disabled = false;
      })
    }
    !this.conf.requestName && this.conf.onclick && this.conf.onclick()
  }
}